#include <QInputDialog>
#include <QDir>
#include "core/magicarray.h"
#include "graphicsview/openfiledialog.h"
#include "graphicsview/neworderdialog.h"
#include "simpledatadiswindow.h"

const std::string SimpleDataDisWindow::s_loggerCat("SimpleDataDis.SimpleDataDisWindow");

SimpleDataDisWindow::SimpleDataDisWindow(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
, m_barWidget(0)
, m_prevWidget(0)
{
    QDir dir;
    if (!dir.exists("Orders"))
        dir.mkdir("Orders");

    setMinimumSize(600, 400);
    setWindowIcon(QIcon(":/SimpleDataDis/Resources/images/icon.png"));
    setAcceptDrops(true);
    setAttribute(Qt::WA_AlwaysShowToolTips);
    setWindowTitle("Freedom");

    createWidgets();
    createAction();
    createToolBar();
    createMenus();

    connect(m_barTabWidget, SIGNAL(currentChanged(int)), this, SLOT(slotTabChanged(int)));

    createConnectionsThis();
    createConnectionsWidget();

    m_barWidget->setCurrency("EURUSD");
    m_barWidget->setPeriod(1440);
    m_barWidget->setRandomDate();
    m_barWidget->setRandomPrice();
    m_barTabWidget->setTabText(0, m_barWidget->getName());
    this->setCentralWidget(m_barTabWidget);
    m_D1Action->setChecked(true);
    mailToUser();
}

SimpleDataDisWindow::~SimpleDataDisWindow()
{
    OrderManager::deinst();
}

void SimpleDataDisWindow::slotTabChanged(int tab)
{
    m_prevWidget = m_barWidget;
    m_barWidget = static_cast<BarWidget*>(m_barTabWidget->currentWidget());
    removeConnectionsWidget();
    createConnectionsWidget();
    m_barWidget->notifyIndex();
}

void SimpleDataDisWindow::slotSetPeriod()
{
    int period = 0;
    if (m_M1Action->isChecked()) {
        period = 1;
        printf("M1\n");
    }
    if (m_M5Action->isChecked()) {
        period = 5;
        printf("M5\n");
    }
    if (m_M15Action->isChecked()) {
        period = 15;
        printf("M15\n");
    }
    if (m_M30Action->isChecked()) {
        period = 30;
        printf("M30\n");
    }
    if (m_H1Action->isChecked()) {
        period = 60;
        printf("H1\n");
    }
    if (m_H4Action->isChecked()) {
        period = 240;
        printf("H4\n");
    }
    if (m_D1Action->isChecked()) {
        period = 1440;
        printf("D1\n");
    }
    if (m_W1Action->isChecked()) {
        period = 10080;
        printf("W1\n");
    }
    if (m_WNAction->isChecked()) {
        period = 43200;
        printf("WN\n");
    }
    if (period != 0)
        m_barWidget->setPeriod(period);
}

void SimpleDataDisWindow::slotOpenCurrency()
{
    OpenFileDialog ofDialog;
    int ok = ofDialog.exec();
    std::string strFolder = OpenFileDialog::getFolder();
    std::string strFile = OpenFileDialog::getFileName();
    //QString str = QInputDialog::getText(this, tr("Currency"),tr("Input"), QLineEdit::Normal,"EURUSD", &ok);
    if (ok == QDialog::Accepted && strFolder.size() != 0) {
        BarWidget *barWidget = new BarWidget(m_barTabWidget);
        int index = m_barTabWidget->addTab(barWidget, barWidget->getName());
        m_barTabWidget->setCurrentIndex(index);
        barWidget->setCurrency(strFolder.c_str());
        if (strFile != "") {
            barWidget->setFilename(strFile.c_str());
        }
        m_D1Action->setChecked(true);
        barWidget->setPeriod(1440);
        m_barTabWidget->setTabText(index, barWidget->getName());
    }
}

void SimpleDataDisWindow::setWindowTitle(const QString& title) {
    m_windowTitle = title;
    QMainWindow::setWindowTitle(m_windowTitle);
}

void SimpleDataDisWindow::mailToUser()
{
    //MagicArray<int> magicarray;
    //m_thread = new SocketThread;
    //m_thread->start();
}

void SimpleDataDisWindow::createMenus()
{
    m_menu = new QMenuBar();
    m_fileMenu = m_menu->addMenu(tr("&File"));
    m_fileMenu->addAction(m_openFileAction);
    setMenuBar(m_menu);
}

void SimpleDataDisWindow::createToolBar()
{
    m_toolBar = new QToolBar("Tool Bar");
    m_toolBar->setIconSize(QSize(20,20));
    m_toolBar->addAction(m_orderListAction);
    m_toolBar->addAction(m_timeSliderAction);
    m_toolBar->addSeparator();

    m_toolBar->addAction(m_zoomInAction);
    m_toolBar->addAction(m_zoomOutAction);
    m_toolBar->addSeparator();

    m_toolBar->addAction(m_M1Action);
    m_toolBar->addAction(m_M5Action);
    m_toolBar->addAction(m_M15Action);
    m_toolBar->addAction(m_M30Action);
    m_toolBar->addAction(m_H1Action);
    m_toolBar->addAction(m_H4Action);
    m_toolBar->addAction(m_D1Action);
    m_toolBar->addAction(m_W1Action);
    m_toolBar->addAction(m_WNAction);
    m_toolBar->addSeparator();

    m_toolBar->addAction(m_drawLineAction);
    m_toolBar->addSeparator();

    addToolBar(m_toolBar);
}

void SimpleDataDisWindow::createWidgets()
{
    m_barTabWidget = new QTabWidget(this);
    m_barWidget = new BarWidget(m_barTabWidget);
    m_barTabWidget->addTab(m_barWidget, m_barWidget->getName());
    m_barTabWidget->setTabPosition(QTabWidget::South);

    m_timeWidget = new TimeControlWidget();
    m_dockTimeWidget = new QDockWidget("TimeSetting", this);
    m_dockTimeWidget->setFloating(true);
    m_dockTimeWidget->setWidget(m_timeWidget);
    m_dockTimeWidget->setAllowedAreas(Qt::NoDockWidgetArea);
    m_dockTimeWidget->show();

    m_ordersWidget = new OrdersWidget();
    addDockWidget(Qt::BottomDockWidgetArea, m_ordersWidget);
}

void SimpleDataDisWindow::createAction()
{
    m_openFileAction = new QAction(QIcon(":/SimpleDataDis/Resources/images/zoomin.png"), tr("&Open"), this);
    m_openFileAction->setStatusTip(tr("Open"));
    m_orderListAction = m_ordersWidget->toggleViewAction();
    m_orderListAction->setIcon(QIcon(":/SimpleDataDis/Resources/images/orderlist.png"));
    m_orderListAction->setText("Order List");
    m_orderListAction->setStatusTip(tr("Open Order List"));
    m_timeSliderAction = m_dockTimeWidget->toggleViewAction();
    m_timeSliderAction->setIcon(QIcon(":/SimpleDataDis/Resources/images/timeslider.png"));
    m_timeSliderAction->setText("Time Slider");
    m_timeSliderAction->setStatusTip(tr("Open Time Slider"));
    m_zoomInAction = new QAction(QIcon(":/SimpleDataDis/Resources/images/zoomin.png"), tr("&Zoom In"), this);
    m_zoomInAction->setStatusTip(tr("Zoom In"));
    m_zoomOutAction = new QAction(QIcon(":/SimpleDataDis/Resources/images/zoomout.png"), tr("&Zoom Out"), this);
    m_zoomOutAction->setStatusTip(tr("Zoom Out"));

    m_M1Action = new QAction(tr("M1"), this);
    m_M1Action->setStatusTip(tr("M1"));
    m_M1Action->setCheckable(true);
    m_M5Action = new QAction(tr("M5"), this);
    m_M5Action->setStatusTip(tr("M5"));
    m_M5Action->setCheckable(true);
    m_M15Action = new QAction(tr("M15"), this);
    m_M15Action->setStatusTip(tr("M15"));
    m_M15Action->setCheckable(true);
    m_M30Action = new QAction(tr("M30"), this);
    m_M30Action->setStatusTip(tr("M30"));
    m_M30Action->setCheckable(true);
    m_H1Action = new QAction(tr("H1"), this);
    m_H1Action->setStatusTip(tr("H1"));
    m_H1Action->setCheckable(true);
    m_H4Action = new QAction(tr("H4"), this);
    m_H4Action->setStatusTip(tr("H4"));
    m_H4Action->setCheckable(true);
    m_D1Action = new QAction(tr("D1"), this);
    m_D1Action->setStatusTip(tr("D1"));
    m_D1Action->setCheckable(true);
    m_W1Action = new QAction(tr("W1"), this);
    m_W1Action->setStatusTip(tr("W1"));
    m_W1Action->setCheckable(true);
    m_WNAction = new QAction(tr("WN"), this);
    m_WNAction->setStatusTip(tr("WN"));
    m_WNAction->setCheckable(true);
    m_timeActionGroup = new QActionGroup(this);
    m_timeActionGroup->addAction(m_M1Action);
    m_timeActionGroup->addAction(m_M5Action);
    m_timeActionGroup->addAction(m_M15Action);
    m_timeActionGroup->addAction(m_M30Action);
    m_timeActionGroup->addAction(m_H1Action);
    m_timeActionGroup->addAction(m_H4Action);
    m_timeActionGroup->addAction(m_D1Action);
    m_timeActionGroup->addAction(m_W1Action);
    m_timeActionGroup->addAction(m_WNAction);

    m_drawLineAction = new QAction(QIcon(":/SimpleDataDis/Resources/images/drawline.png"), tr("&Draw Line"), this);
    m_drawLineAction->setCheckable(true);
    m_drawLineAction->setChecked(true);
    m_drawLineAction->setStatusTip(tr("Draw Line"));
}

void SimpleDataDisWindow::createConnectionsWidget()
{
    connect(m_timeWidget, SIGNAL(signalJumpToTime(int)), m_barWidget, SLOT(slotJumpToTime(int)));
    connect(m_timeWidget, SIGNAL(signalSetToTime(int)), m_barWidget, SLOT(slotSetToTime(int)));
    connect(m_timeWidget, SIGNAL(signalBarIndexChanged(int)), m_barWidget, SLOT(slotSetHistIndex(int)));
    connect(m_timeWidget, SIGNAL(signalUserIndexChanged(int)), m_barWidget, SLOT(slotSetUserIndex(int)));
    connect(m_timeWidget, SIGNAL(signalBuySignal()), m_barWidget, SLOT(slotBuyOrder()));
    connect(m_timeWidget, SIGNAL(signalSellSignal()), m_barWidget, SLOT(slotSellOrder()));
    connect(m_barWidget, SIGNAL(signalChangedHistIndex(int, int)), m_timeWidget, SLOT(slotSetHistIndexRange(int, int)));
    connect(m_barWidget, SIGNAL(signalChangedUserIndex(int, int)), m_timeWidget, SLOT(slotSetUserIndexRange(int, int)));
    connect(m_barWidget, SIGNAL(signalSetHistIndex(int)), m_timeWidget, SLOT(slotSetHisIndex(int)));
    connect(m_barWidget, SIGNAL(signalSetUserIndex(int)), m_timeWidget, SLOT(slotSetUserIndex(int)));
    connect(m_barWidget, SIGNAL(signalChangedHistIndex(int)), m_timeWidget, SLOT(slotChangedHisIndex(int)));
    connect(m_barWidget, SIGNAL(signalChangedUserIndex(int)), m_timeWidget, SLOT(slotChangedUserIndex(int)));

    connect(m_zoomInAction, SIGNAL(triggered()), m_barWidget, SLOT(slotIncBarSize()));
    connect(m_zoomOutAction, SIGNAL(triggered()), m_barWidget, SLOT(slotDecBarSize()));

    connect(m_drawLineAction, SIGNAL(toggled(bool)), m_barWidget, SLOT(slotSetDrawLine(bool)));

}

void SimpleDataDisWindow::createConnectionsThis()
{
    connect(m_M1Action, SIGNAL(triggered()), this, SLOT(slotSetPeriod()));
    connect(m_M5Action, SIGNAL(triggered()), this, SLOT(slotSetPeriod()));
    connect(m_M15Action, SIGNAL(triggered()), this, SLOT(slotSetPeriod()));
    connect(m_M30Action, SIGNAL(triggered()), this, SLOT(slotSetPeriod()));
    connect(m_H1Action, SIGNAL(triggered()), this, SLOT(slotSetPeriod()));
    connect(m_H4Action, SIGNAL(triggered()), this, SLOT(slotSetPeriod()));
    connect(m_D1Action, SIGNAL(triggered()), this, SLOT(slotSetPeriod()));
    connect(m_W1Action, SIGNAL(triggered()), this, SLOT(slotSetPeriod()));
    connect(m_WNAction, SIGNAL(triggered()), this, SLOT(slotSetPeriod()));

    connect(m_openFileAction, SIGNAL(triggered()), this, SLOT(slotOpenCurrency()));
}

void SimpleDataDisWindow::removeConnectionsWidget()
{
    if (m_prevWidget) {
        m_prevWidget->disconnect();
        m_timeWidget->disconnect();
    }
}

void SimpleDataDisWindow::keyPressEvent(QKeyEvent *event)
{
    /*if (event->key() == Qt::Key_O) {
        NewOrderDialog *newOrderWidget = new NewOrderDialog("EURUSD", 0, (2020 - 1970) * 365 * 24 * 3600, 0, 0);
        newOrderWidget->exec();
    }*/
}